﻿Imports System.IO

Public Class frmViewImageCase

    Public _CaseID As Long
    Public _isFileCasePlaintiffID As Boolean = False
    Public _isFileCaseDefendantID As Boolean = False
    Public _isFileCaseThirdPartyID As Boolean = False
    Public _FileCasePlaintiffID As Long
    Public _FileCaseDefendantID As Long
    Public _FileCaseThirdPartyID As Long

    Dim dt As New DataTable
    Dim j As Integer = 0
    Dim tmp As Integer

    Private Sub frmViewImage_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        j = 0
        If _isFileCasePlaintiffID Then
            getImage_Case_PlaintiffID()
        ElseIf _isFileCaseDefendantID Then
            getImage_Case_DefendantID()
        ElseIf _isFileCaseThirdPartyID Then
            getImage_Case_ThirdPartyID()
        End If
    End Sub

    Protected Sub getImage_Case_PlaintiffID()
        Try
            getImage_CasePlaintiff()
            Dim i As Integer
            If dt.Rows.Count > 0 Then
                If _FileCasePlaintiffID <> 0 Then
                    For i = 0 To dt.Rows.Count - 1
                        If _FileCasePlaintiffID = dt.Rows(i)("FileCasePlaintiffID") Then
                            lblUserID.Text = dt.Rows(i)("UserID")
                            lblUploadDate.Text = Format(dt.Rows(i)("UploadDate"), "dd-MM-yyyy hh:mm tt")
                            lblNo.Text = i + 1 & " of " & dt.Rows.Count
                            'Image
                            Dim pic As Byte()
                            pic = dt.Rows(i)("FileUpload")
                            Dim streamPhoto As New MemoryStream(pic, True)
                            Dim bmp As Bitmap = New Bitmap(streamPhoto)
                            picImage.Image() = New Bitmap(bmp)
                            streamPhoto.Close()
                            Exit For
                        End If
                        j += 1
                    Next
                Else
                    MessageBox.Show("គ្មានរូបភាពបង្ហាញទេ", "បង្ហាញរូបភាពឯកសារ", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub getImage_Case_DefendantID()
        Try
            getImage_CaseDefendant()
            Dim i As Integer
            If dt.Rows.Count > 0 Then
                If _FileCaseDefendantID <> 0 Then
                    For i = 0 To dt.Rows.Count - 1
                        If _FileCaseDefendantID = dt.Rows(i)("FileCaseDefendantID") Then
                            lblUserID.Text = dt.Rows(i)("UserID")
                            lblUploadDate.Text = Format(dt.Rows(i)("UploadDate"), "dd-MM-yyyy hh:mm tt")
                            lblNo.Text = i + 1 & " of " & dt.Rows.Count
                            'Image
                            Dim pic As Byte()
                            pic = dt.Rows(i)("FileUpload")
                            Dim streamPhoto As New MemoryStream(pic, True)
                            Dim bmp As Bitmap = New Bitmap(streamPhoto)
                            picImage.Image() = New Bitmap(bmp)
                            streamPhoto.Close()
                            Exit For
                        End If
                        j += 1
                    Next
                Else
                    MessageBox.Show("គ្មានរូបភាពបង្ហាញទេ", "បង្ហាញរូបភាពឯកសារ", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub getImage_Case_ThirdPartyID()
        Try
            getImage_CaseThirdParty()
            Dim i As Integer
            If dt.Rows.Count > 0 Then
                If _FileCaseThirdPartyID <> 0 Then
                    For i = 0 To dt.Rows.Count - 1
                        If _FileCaseThirdPartyID = dt.Rows(i)("FileCaseThirdPartyID") Then
                            lblUserID.Text = dt.Rows(i)("UserID")
                            lblUploadDate.Text = Format(dt.Rows(i)("UploadDate"), "dd-MM-yyyy hh:mm tt")
                            lblNo.Text = i + 1 & " of " & dt.Rows.Count
                            'Image
                            Dim pic As Byte()
                            pic = dt.Rows(i)("FileUpload")
                            Dim streamPhoto As New MemoryStream(pic, True)
                            Dim bmp As Bitmap = New Bitmap(streamPhoto)
                            picImage.Image() = New Bitmap(bmp)
                            streamPhoto.Close()
                            Exit For
                        End If
                        j += 1
                    Next
                Else
                    MessageBox.Show("គ្មានរូបភាពបង្ហាញទេ", "បង្ហាញរូបភាពឯកសារ", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub getImage_Case_Move()
        If j >= 0 And j < dt.Rows.Count Then
            lblUserID.Text = dt.Rows(j)("UserID")
            lblUploadDate.Text = Format(dt.Rows(j)("UploadDate"), "dd-MM-yyyy hh:mm tt")
            lblNo.Text = j + 1 & " of " & dt.Rows.Count
            'Image
            Dim pic As Byte()
            pic = dt.Rows(j)("FileUpload")
            Dim streamPhoto As New MemoryStream(pic, True)
            Dim bmp As Bitmap = New Bitmap(streamPhoto)
            picImage.Image() = New Bitmap(bmp)
            streamPhoto.Close()
        Else
            j = tmp
        End If
    End Sub

    Protected Sub getImage_CasePlaintiff()
        Dim oFile As New cFile
        dt = oFile.select_File_CasePlaintiff_byCaseID(_CaseID)
    End Sub

    Protected Sub getImage_CaseDefendant()
        Dim oFile As New cFile
        dt = oFile.select_File_CaseDefendant_byCaseID(_CaseID)
    End Sub

    Protected Sub getImage_CaseThirdParty()
        Dim oFile As New cFile
        dt = oFile.select_File_CaseThirdParty_byCaseID(_CaseID)
    End Sub

    Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
        tmp = j
        j = 0
        getImage_Case_Move()
    End Sub

    Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        tmp = j
        j -= 1
        getImage_Case_Move()
    End Sub

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        tmp = j
        j += 1
        getImage_Case_Move()
    End Sub

    Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
        tmp = j
        j = dt.Rows.Count - 1
        getImage_Case_Move()
    End Sub
End Class